#include "window_common.h"
#include "gkt_door.h"

#if !GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_FIXED

enum {
	WIDGET_ID_LIST = 0,
	WIDGET_ID_MAX
};

enum {
	TIMER_ID_TIMEOUT = 0,
	TIMER_ID_MAX
};

static const gkt_widget_list_item_s sc_window_lock_open_direction_widget_list_items[] = {
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_VOICE_COMBINED_ID(set_1), 
			GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_FORWARD),
	GKT_WIDGET_LIST_STATIC_ITEM(WINDOW_VOICE_COMBINED_ID(set_2), 
			GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_REVERSE),
};

#define WINDOW_LOCK_OPEN_DIRECTION_LIST_ITEM_NUMS	\
	sizeof(sc_window_lock_open_direction_widget_list_items) / sizeof(gkt_widget_list_item_s)

static const gkt_widget_list_attr_s sc_window_lock_open_direction_list_attr = 
	GKT_WIDGET_LIST_STATIC_ATTR(WINDOW_LOCK_OPEN_DIRECTION_LIST_ITEM_NUMS, 
		sc_window_lock_open_direction_widget_list_items);

GKT_WINDOW_WIDGET_DESCS_DEFINE(lock_open_direction) = {
	[WIDGET_ID_LIST] = GKT_WIDGET_LIST_DESC(0, &sc_window_lock_open_direction_list_attr),
};

typedef struct _window_motor_rotate_direction {
	gkt_door_lock_motor_rotate_direction_e	direction;
	uint32_t play_over;
} window_motor_rotate_direction_s;
static window_motor_rotate_direction_s s_window_motor_rotate_direction;

static void window_lock_open_direction_completed(window_result_e result)
{
	uint32_t voice_flags = GKT_WINDOW_VOICE_F_NOTIFY_END | GKT_WINDOW_VOICE_F_NOTIFY_ERROR;

	if (WINDOW_RESULT_SET_SUCCESS == result) {
		gp_window_unreset_data->lock_motor_rotate_direction = 
			s_window_motor_rotate_direction.direction;
		gp_window_unreset_data->lock_motor_rotate_direction_set = 1;
		window_data_save();

		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_success), voice_flags, NULL);
	}
	else {
		gkt_window_voice_play(NULL, WINDOW_VOICE_COMBINED_ID(set_failed), voice_flags, NULL);
	}
	s_window_motor_rotate_direction.play_over = 1;
}

static void window_lock_open_direction_cfg_update(void)
{
	int retval;
	window_common_mask_all();

	retval = gkt_door_lock_set_motor_rotate_direction(s_window_motor_rotate_direction.direction);
	if (GKT_SUCCESS == retval)
		window_lock_open_direction_completed(WINDOW_RESULT_SET_SUCCESS);
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
	else if (GKT_EINPROCESS == retval)
		gkt_window_timer_set(TIMER_ID_TIMEOUT, 2000, 0, 0);
#endif
	else
		window_lock_open_direction_completed(WINDOW_RESULT_SET_FAILED);
}

static int window_lock_open_direction_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_CONTINUE;

	switch(major_type) {
		case GKT_EVENT_WINDOW:
			if (GKT_WINDOW_ACTIVE == minor_type) {
				s_window_motor_rotate_direction.direction = GKT_DOOR_LOCK_MOTOR_ROTATE_DIRECTION_FORWARD;
				s_window_motor_rotate_direction.play_over = 0;
				window_common_mask_except_number_repeat_return(WINDOW_LOCK_OPEN_DIRECTION_LIST_ITEM_NUMS);	
			}
			break;
		case GKT_EVENT_WIDGET:
			if (GKT_WIDGET_EVENT_CLICK == minor_type) {
				s_window_motor_rotate_direction.direction = sc_window_lock_open_direction_widget_list_items[data].data;
				window_lock_open_direction_cfg_update();
				retval = GKT_EVENT_HANDLE_COMPLETE;
			}
			break;
		case GKT_EVENT_TIMER:
			window_lock_open_direction_completed(WINDOW_RESULT_SET_FAILED);
			break;
		case GKT_EVENT_SYSTEM:
			if (GKT_WINDOW_SYSTEM_DOOR == minor_type) {
#if (GKT_MODULE_LOC_BACKBOARD == GKT_DOOR_LOCK_LOCATION)
				if (GKT_DOOR_NOTIFY_LOCK_CONFIG == id) {
					gkt_window_timer_clear(TIMER_ID_TIMEOUT);
					if (GKT_SUCCESS == data)
						window_lock_open_direction_completed(WINDOW_RESULT_SET_SUCCESS);
					else
						window_lock_open_direction_completed(WINDOW_RESULT_SET_FAILED);

					return GKT_EVENT_HANDLE_COMPLETE;
				}
#endif
			}
			break;
		case GKT_EVENT_VOICE:
			if (s_window_motor_rotate_direction.play_over == 1) {
				s_window_motor_rotate_direction.play_over = 0;
				gkt_window_exit();
			}
			break;
		default:
			break;
	}

	return retval;
}
GKT_WINDOW_DESC_DEFINE(lock_open_direction);

#endif

